package com.b2b.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.PlatformFinancialFlowDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.PlatformFinancialFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/platformFinancialFlow")
@PropertySource("classpath:config.properties")
public class PlatformFinancialFlowController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final PlatformFinancialFlowService platformFinancialFlowService;

    @Autowired
    public PlatformFinancialFlowController(PlatformFinancialFlowService platformFinancialFlowService){
        this.platformFinancialFlowService = platformFinancialFlowService;
    }

    @PostMapping
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add(@RequestBody PlatformFinancialFlowDto platformFinancialFlowDto) {
        int count = platformFinancialFlowService.insert(platformFinancialFlowDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", platformFinancialFlowDto);
    }

    @GetMapping
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findAll(@Validated @RequestBody BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<PlatformFinancialFlowDto> platformFinancialFlowDtos = platformFinancialFlowService.selectAll();
        PageInfo<PlatformFinancialFlowDto> selectPage = new PageInfo<PlatformFinancialFlowDto>(platformFinancialFlowDtos);
        if (platformFinancialFlowDtos == null || platformFinancialFlowDtos.size() < 0) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/byId")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(@RequestBody PlatformFinancialFlowDto platformFinancialFlowDto) {
        platformFinancialFlowDto = platformFinancialFlowService.selectByPrimaryKey(platformFinancialFlowDto.getFinancialId());
        if (platformFinancialFlowDto == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", platformFinancialFlowDto);
    }

    @PutMapping
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody PlatformFinancialFlowDto platformFinancialFlowDto) {
        PlatformFinancialFlowDto platformFinancialFlowDtoTemp = new PlatformFinancialFlowDto();
        platformFinancialFlowDtoTemp.setFinancialId(platformFinancialFlowDto.getFinancialId());
        platformFinancialFlowDtoTemp = platformFinancialFlowService.selectOne(platformFinancialFlowDtoTemp);
        if (platformFinancialFlowDtoTemp == null) {
            throw new CustomUnauthorizedException("该帐号不存在(Account not exist.)");
        }
        int count = platformFinancialFlowService.updateByPrimaryKeySelective(platformFinancialFlowDto);

        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", platformFinancialFlowDto);
    }

    @DeleteMapping
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestBody PlatformFinancialFlowDto platformToBepaidDto) {
        Integer id = platformToBepaidDto.getFinancialId();
        int count = platformFinancialFlowService.deleteByPrimaryKey(id);
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
